package com.eastseeway.radioproject.database;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;
import androidx.room.migration.Migration;
import androidx.sqlite.db.SupportSQLiteDatabase;

import com.eastseeway.radioproject.entities.BeanOfElectrode;
import com.eastseeway.radioproject.entities.BeanVBus;
import com.eastseeway.radioproject.entities.OperationParams;
import com.eastseeway.radioproject.entities.UserInfo;
import com.eastseeway.radioproject.entities.VersionBean;

@Database(entities = {UserInfo.class, OperationParams.class, VersionBean.class, BeanOfElectrode.class, BeanVBus.class},version = 1,exportSchema = false)
public abstract class RadioProjectDatabase extends RoomDatabase {
    private static RadioProjectDatabase instance;
    public synchronized static RadioProjectDatabase getInstance(Context context){
        if (instance == null) {
            instance = Room.databaseBuilder(context.getApplicationContext(), RadioProjectDatabase.class,"radio_project_database")
                    .fallbackToDestructiveMigration()//这句话是说迁移数据库时破坏式的，原来的数据全被删除，修改数据结构->修改版本号->加上这句话
//                    .addMigrations(migration_1_2)
                    .build();
        }
        return instance;
    }
    public abstract UserInfoDao getUserInfoDao();

    public abstract VersionInfoDao getVersionInfoDao();

    public abstract ElectrodeDao getElectrodeDao();

    public abstract VBusDao getVBusDao();

    static final Migration migration_1_2 = new Migration( 1,2){
        @Override
        public void migrate(@NonNull SupportSQLiteDatabase supportSQLiteDatabase) {
            supportSQLiteDatabase.execSQL("alter table userinfo add column bar_data integer not null default 1");
        }
    };
}
